<?php
/*
+--------------------------------------------------------------------------
|   AffiliStore 2
|   ========================================
|   Web: http://www.affilistore.com
|   Email: admin (at) affilistore (dot) com
|	License Type: AffiliStore 2 is NOT open source software and limitations apply 
|   Licence Info: Visit AffiliStore website and click on 'Licence'
+--------------------------------------------------------------------------
*/
// checks to see if password session is set
// if not redirects to ../admin/
if($_SESSION['loggedIn'] == 0 && !isset($adminPage)){
include('../includes/auth.inc.php');
}


// include fck wysiwyg editor
include("includes/rte/fckeditor.php");



// add a category
if (isset($_POST['pd_updated'])) {

	// check to see if details have been completed
	if (empty($_POST['addThisPage'])) {
	$error = 1;

	// insert new page
	} else {

	$sql =  sprintf("INSERT INTO affiliSt_pages (name, parent, content, location) values (%s, %s, %s, %s)",
			   quote_smart($_POST['addThisPage']),
			   quote_smart($_POST['parentPage']),
			   quote_smart($_POST['addContent']),
			   quote_smart($_POST['linklocation1'].':'.$_POST['linklocation2'].':'.$_POST['linklocation3']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
		
		// add one to parent subCategories counter
		if ($_POST['parentPage'] != 'noparent') {
		$sql =  sprintf("UPDATE affiliSt_pages SET subCategories = subCategories + 1 WHERE id = %s",
				   quote_smart($_POST['parentPage']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
		}
	
	}
}












// delete a category
if (isset($_GET['delete'])) {
$sql =  sprintf("DELETE FROM affiliSt_pages WHERE id = %s",
           quote_smart($_GET['delete']));
mysql_query($sql, $databaseConnect) or die(mysql_error());

	// subtract one from parent subCategories counter
	if (isset($_GET['p'])) {
	$sql =  sprintf("UPDATE affiliSt_pages SET subCategories = subCategories - 1 WHERE id = %s",
				quote_smart($_GET['p']));
	mysql_query($sql, $databaseConnect) or die(mysql_error());
	}
}












// edit a page
if (isset($_POST['pd_edited'])) {

	// check to see if details have been completed
		if (empty($_POST['editThisPage']) && $_POST['editID'] != 1) {
		$error = 1;
	
		// edit page
		} else {
	
		$sql =  sprintf("UPDATE affiliSt_pages SET name = %s, parent = %s, content = %s, location = %s WHERE id = %s",
				   quote_smart($_POST['editThisPage']),
				   quote_smart($_POST['parentPage']),
				   quote_smart($_POST['editContent']),
				   quote_smart($_POST['linklocation1'].':'.$_POST['linklocation2'].':'.$_POST['linklocation3']),
				   quote_smart($_POST['editID']));
		mysql_query($sql, $databaseConnect) or die(mysql_error());
			
			if ($_POST['parentPage'] != 'noparent' && $_POST['parentPage'] != $_POST['oldParent']) {
			
			// add new
			$sql =  sprintf("UPDATE affiliSt_pages SET subCategories = subCategories + 1 WHERE id = %s",
						quote_smart($_POST['parentPage']));
			mysql_query($sql, $databaseConnect) or die(mysql_error());
			
			// minus old
			$sql =  sprintf("UPDATE affiliSt_pages SET subCategories = subCategories - 1 WHERE id = %s",
						quote_smart($_POST['oldParent']));
			mysql_query($sql, $databaseConnect) or die(mysql_error());
			
			} else if  ($_POST['parentPage'] == 'noparent') {
			
			// minus old
			$sql =  sprintf("UPDATE affiliSt_pages SET subCategories = subCategories - 1 WHERE id = %s",
						quote_smart($_POST['oldParent']));
			mysql_query($sql, $databaseConnect) or die(mysql_error());
			
			}
		
		}
}










$getCatNames = mysql_query("SELECT * FROM affiliSt_pages ORDER BY name ASC");
$catNames = mysql_fetch_assoc($getCatNames);

$getTopCat = mysql_query("SELECT * FROM affiliSt_pages ORDER BY name ASC");
$topCat = mysql_fetch_assoc($getTopCat);
?>



<?php
// error report
if ($error == 1) {
echo '<fieldset><strong style="color:red">ERROR: Fields must not be empty</strong></fieldset>';
} else {
	// category added report
	if (isset($_POST['pd_updated'])) {
	echo '<fieldset><strong style="color:green">Page Added</strong></fieldset>';
	} 
	
	// category edited report
	if (isset($_POST['pd_edited'])) {
	echo '<fieldset><strong style="color:green">Page Edited</strong></fieldset>';
	}
}

?>






<?php
// edit category html
if (isset($_GET['edit'])) {


$sql =  sprintf("SELECT * FROM affiliSt_pages WHERE id = %s",
           quote_smart($_GET['edit']));
$getEditCat = mysql_query($sql, $databaseConnect) or die(mysql_error());
$editCat = mysql_fetch_assoc($getEditCat);

// get link location options
$linkOpt = explode(":", $editCat['location']);
?>

<fieldset style="border: 1px solid green;">
<legend>Edit Page</legend>
<form action="index.php?ad=pd" method="post" name="editDetails">
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td valign="top" align="left">
<label for="edit_page">Page Name/Title: </label>
</td>
<td valign="top" align="left">
<input name="editThisPage" type="text" class="adminInput" id="edit_page" <?php if ($_GET['edit'] == 1) { echo 'disabled="disabled" value="Use editor below to create home title"'; } else { ?> value="<?php echo $editCat['name'] ?>" <?php } ?> />
</td>
</tr>
<tr>
<td valign="top" align="left">
<label for="parent_page">Parent Page: </label>
</td>
<td valign="top" align="left">
<select name="parentPage" id="parent_page" />
<option value="noparent">No Parent</option>
<?php 
// repeat cat names options
do {
		if (($catNames['id'] !== NULL && $catNames['parent'] == 0 && $editCat['subCategories'] == 0 && $editCat['id'] != $catNames['id'] && $catNames['id'] != 1) && $_GET['edit'] != 1) { ?>
		<option value="<?php echo $catNames['id'] ?>"
		<?php if ($editCat['parent'] == $catNames['id']) {
		echo ' selected="selected" ';
			}
?>
><?php echo $catNames['name'] ?></option>
<?php 	}
} while ($catNames = mysql_fetch_assoc($getCatNames)); ?>
</select>
</td>
</tr>
<?php if ($_GET['edit'] != 1) { ?>
<tr>
<td valign="top" align="left">
<label for="link_location">Page Link Location: </label>
</td>
<td valign="top" align="left">
<input name="linklocation1" id="link_location" type="checkbox" value="1" <? if ($linkOpt[0] == 1) { echo 'checked="checked"'; } ?> /> Top Navigation<br />
<input name="linklocation2" type="checkbox" value="1" <? if ($linkOpt[1] == 1) { echo 'checked="checked"'; } ?> /> Side Navigation<br />
<input name="linklocation3" type="checkbox" value="1" <? if ($linkOpt[2] == 1) { echo 'checked="checked"'; } ?> /> Footer Links<br />
</td>
</tr>
<?php } ?>
<tr>
<td valign="top" align="left" colspan="2">
<?php
$oFCKeditor = new FCKeditor('editContent');
$oFCKeditor->BasePath = 'includes/rte/';
$oFCKeditor->Value = $editCat['content'];
$oFCKeditor->Create();
?>
</td>
</tr>
</table>
<input type="hidden" name="editID" value="<?php echo $_GET['edit']; ?>" />
<input type="hidden" name="oldParent" value="<?php echo $_GET['p']; ?>" />
<input type="hidden" name="pd_edited" value="yes" />
<input value="Edit Page" type="submit" class="padSubmit" />
</form>
</fieldset>













<?php } else { 
// add page html
?>

<fieldset>
<legend>Add Page</legend>
<form action="index.php?ad=pd" method="post" name="updateDetails">
<table cellpadding="4" cellspacing="0" border="0" width="100%">
<tr>
<td valign="top" align="left">
<label for="add_page">Page Name/Title: </label>
</td>
<td valign="top" align="left">
<input name="addThisPage" type="text" class="adminInput" id="add_page" />
</td>
</tr>
<tr>
<td valign="top" align="left">
<label for="parent_page">Parent Page: </label>
</td>
<td valign="top" align="left">
<select name="parentPage" id="parent_page" />
<option value="noparent" selected="selected">No Parent</option>
<?php 
// repeat cat names options
do { 
	if ($catNames['id'] !== NULL && $catNames['parent'] == 0 && $catNames['id'] != 1) { ?>
	<option value="<?php echo $catNames['id'] ?>"><?php echo $catNames['name'] ?></option>
<?php }
} while ($catNames = mysql_fetch_assoc($getCatNames)); ?>
</select>
</td>
</tr>
<tr>
<td valign="top" align="left">
<label for="link_location">Page Link Location: </label>
</td>
<td valign="top" align="left">
<input name="linklocation1" id="link_location" type="checkbox" value="1" /> Top Navigation<br />
<input name="linklocation2" type="checkbox" value="1" checked="checked" /> Side Navigation<br />
<input name="linklocation3" type="checkbox" value="1" /> Footer Links<br />
</td>
</tr>
<tr>
<td valign="top" align="left" colspan="2">
<?php
$oFCKeditor = new FCKeditor('addContent');
$oFCKeditor->BasePath = 'includes/rte/';
$oFCKeditor->Value = "";
$oFCKeditor->Create();
?>
</td>
</tr>
</table>
<input type="hidden" name="pd_updated" value="yes" />
<input value="Add Page" type="submit" class="padSubmit" />
</form>
</fieldset>

<?php } ?>











<fieldset>
<legend>Current Pages</legend>

<strong>Home</strong> [<a href="index.php?ad=pd&edit=1">edit</a>]<br />

<?php 
// repeat category listings
do { 
	if ($topCat['id'] !== NULL && $topCat['parent'] == 0 && $topCat['id'] != 1) {
	echo '<br /><strong>'.$topCat['name'].'</strong>
	[<a href="index.php?ad=pd&edit='.$topCat['id'].'">edit</a>] ';
		
		// delete if category has no links
		if ($topCat['counter'] == 0 && $topCat['subCategories'] == 0) {
		echo '[<a href="index.php?ad=pd&delete='.$topCat['id'].'">delete</a>]<br />';
		} else {
		echo '[<span style="color:grey">delete</span>]<br />';
		}

		$getSubCat = mysql_query("SELECT * FROM affiliSt_pages ORDER BY parent ASC, name ASC");
		$subCat = mysql_fetch_assoc($getSubCat);
	
		// repeat sub category listings
		do { 
			
			if ($subCat['parent'] == $topCat['id']) {
			echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;'.$subCat['name'].'
			[<a href="index.php?ad=pd&edit='.$subCat['id'].'&p='.$subCat['parent'].'">edit</a>] ';
		
				// delete if category has no links
				if ($subCat['counter'] == 0) {
				echo '[<a href="index.php?ad=pd&delete='.$subCat['id'].'&p='.$subCat['parent'].'">delete</a>]<br />';
				} else {
				echo '[<span style="color:grey">delete</span>]<br />';
				}
			}
		} while ($subCat = mysql_fetch_assoc($getSubCat));
		
		
	}
} while ($topCat = mysql_fetch_assoc($getTopCat)); ?>
</fieldset>